package com.zyn.rsgl.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zyn.rsgl.mapper.BulletinMapper;
import com.zyn.rsgl.pojo.Bulletin;
import com.zyn.rsgl.service.BulletinService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyn.rsgl.utils.commonResult.R;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 公告表 服务实现类
 * </p>
 *
 * @author zcs
 * @since 2025-02-25
 */
@Service
public class BulletinServiceImpl extends ServiceImpl<BulletinMapper, Bulletin> implements BulletinService {
    @Override
    public R listPage(Integer pageNum, Integer pageSize, String title, String timeStr) {
        // 创建查询器
        QueryWrapper<Bulletin> queryWrapper = new QueryWrapper<>();
        // 处理时间参数转换
        if (timeStr != null && !timeStr.isEmpty()) {
            // 将 LocalDateTime 转换为 LocalDate
            LocalDate startDate = LocalDate.parse(timeStr);
            queryWrapper.ge("time", startDate.atStartOfDay()); // 大于等于当天的开始时间
            queryWrapper.le("time", startDate.atTime(23, 59, 59)); // 小于等于当天的结束时间
        }
        if (title != null) {
            queryWrapper.like("title", title);
        }
        //        增加排序
        queryWrapper.orderBy(true,false,"time");
        // 初始化分页查询
        Page<Object> page = PageHelper.startPage(pageNum, pageSize);
        // 执行查询
        List<Bulletin> list = this.list(queryWrapper);
        if (list != null) {
            return R.ok(list, page.getTotal());
        }
        return R.fail("查询失败");
    }
}
